﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SeatSet
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            
            var updater = FSLib.App.SimpleUpdater.Updater.Instance;
            FSLib.App.SimpleUpdater.Updater.CheckUpdateSimple();
            RegFile();//注册文件类型            
            if (args.Length > 0)
            {
                Application.Run(new Main(args));
            }
            else
            {
                Application.Run(new Main());
            }
        }

        private static void RegFile()
        {
            string keyName;
            string keyValue;
            keyName = "SeatChartFile";
            keyValue = "座次表文件";
            RegistryKey isExCommand = null;
            bool isCreateRegistry = true;
            try
            {
                /// 检查 文件关联是否创建
                isExCommand = Registry.ClassesRoot.OpenSubKey(keyName);
                if (isExCommand == null)
                {
                    isCreateRegistry = true;
                }
                else
                {
                    if (isExCommand.GetValue("Create").ToString() == Application.ExecutablePath.ToString())
                    {
                        isCreateRegistry = false;
                    }
                    else
                    {
                        Registry.ClassesRoot.DeleteSubKeyTree(keyName);
                        isCreateRegistry = true;
                    }
                }
            }
            catch (Exception)
            {
                isCreateRegistry = true;
            }
            /// 假如 文件关联 还没有创建，或是关联位置已被改变
            if (isCreateRegistry)
            {
                try
                {
                    RegistryKey key, keyico;
                    key = Registry.ClassesRoot.CreateSubKey(keyName);
                    key.SetValue("Create", Application.ExecutablePath.ToString());
                    keyico = key.CreateSubKey("DefaultIcon");
                    keyico.SetValue("", Application.ExecutablePath + ",0");
                    key.SetValue("", keyValue);
                    key = key.CreateSubKey("Shell");
                    key = key.CreateSubKey("Open");
                    key = key.CreateSubKey("Command");
                    /// 关联的位置
                    key.SetValue("", "\"" + Application.ExecutablePath.ToString() + "\" \"%1\"");
                    /// 关联的文件扩展名, 
                    keyName = ".sscf";
                    keyValue = "SeatChartFile";
                    key = Registry.ClassesRoot.CreateSubKey(keyName);
                    key.SetValue("", keyValue);
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
